/*******************************************************************************

This code file produces Table 2, "Summary Statistics on 421-a Eligible Buildings, 2003--2015."

*******************************************************************************/

*** Manage settings

	run "$dir/code/modules/settings.do"
	
* Load Stata-TeX

	do "$code/modules/stata-tex.do"
	cd "$tables/estimates"
	
*** Load data

	use "$data/clean/cleaned_data.dta", clear
	
********************************************************************************
* Table-specific data cleaning
********************************************************************************
	
	* DOF market val of buildings in $ millions
	gen mvalue = assesstot/(frac_assess*underassess)
	replace mvalue = mvalue/1000000
	
	gen mvalue_land = assessland/(frac_assess*underassess)
	replace mvalue_land = mvalue_land/1000000
	
	gen mvalue_bldg = (assesstot-assessland)/(frac_assess*underassess)
	replace mvalue_bldg = mvalue_bldg/1000000
	
	gen mvalue_per_unit = mvalue/unitsres
	
	* Potential tax savings ($)
	gen tax_savings_per_unit = 1000000*dtaxrate_onsite*mvalue/(unitsres*0.2)
	gen tax_savings = 1000000*dtaxrate_onsite*mvalue
	
	* Rental or owner-occupied
	gen rental = 1 - condo
	
	* Rescale dtaxrate_onsite, taxrate (to p.p.)
	replace dtaxrate_onsite = dtaxrate_onsite*100
	replace taxrate = taxrate*100
	
	
********************************************************************************
* Complete table analysis
********************************************************************************

	local varlist = "mvalue mvalue_land mvalue_bldg mvalue_per_unit dtaxrate_onsite tax_savings tax_savings_per_unit unitstotal unitsres numfloors rental taxrate"

	tabstat `varlist', by(inclusionary_onsite) stats(mean sd) col(stat) save nototal
		
	mat stats1 = r(Stat1)
	mat stats2 = r(Stat2)
	
	* mvalue
	local v1 = stats1[1,1]
	local v2 = stats2[1,1]
	local v3 = stats1[2,1]
	local v4 = stats2[2,1]
	insert_into_file using table2.csv, key(mvalue0_mean) value(`v1') format(%12.2f)
	insert_into_file using table2.csv, key(mvalue1_mean) value(`v2') format(%12.2f)
	insert_into_file using table2.csv, key(mvalue0_sd) value(`v3') format(%12.2f)
	insert_into_file using table2.csv, key(mvalue1_sd) value(`v4') format(%12.2f)
	
	* mvalue_land
	local v1 = stats1[1,2]
	local v2 = stats2[1,2]
	local v3 = stats1[2,2]
	local v4 = stats2[2,2]
	insert_into_file using table2.csv, key(mvalue_land0_mean) value(`v1') format(%12.2f)
	insert_into_file using table2.csv, key(mvalue_land1_mean) value(`v2') format(%12.2f)
	insert_into_file using table2.csv, key(mvalue_land0_sd) value(`v3') format(%12.2f)
	insert_into_file using table2.csv, key(mvalue_land1_sd) value(`v4') format(%12.2f)
	
	* mvalue_bldg
	local v1 = stats1[1,3]
	local v2 = stats2[1,3]
	local v3 = stats1[2,3]
	local v4 = stats2[2,3]
	insert_into_file using table2.csv, key(mvalue_bldg0_mean) value(`v1') format(%12.2f)
	insert_into_file using table2.csv, key(mvalue_bldg1_mean) value(`v2') format(%12.2f)
	insert_into_file using table2.csv, key(mvalue_bldg0_sd) value(`v3') format(%12.2f)
	insert_into_file using table2.csv, key(mvalue_bldg1_sd) value(`v4') format(%12.2f)
	
	* mvalue_per_unit
	local v1 = stats1[1,4]
	local v2 = stats2[1,4]
	local v3 = stats1[2,4]
	local v4 = stats2[2,4]
	insert_into_file using table2.csv, key(mvalue_per_unit0_mean) value(`v1') format(%12.3f)
	insert_into_file using table2.csv, key(mvalue_per_unit1_mean) value(`v2') format(%12.3f)
	insert_into_file using table2.csv, key(mvalue_per_unit0_sd) value(`v3') format(%12.3f)
	insert_into_file using table2.csv, key(mvalue_per_unit1_sd) value(`v4') format(%12.3f)
	
	* dtaxrate_onsite
	local v1 = stats1[1,5]
	local v2 = stats2[1,5]
	local v3 = stats1[2,5]
	local v4 = stats2[2,5]
	insert_into_file using table2.csv, key(dtaxrate_onsite0_mean) value(`v1') format(%12.2f)
	insert_into_file using table2.csv, key(dtaxrate_onsite1_mean) value(`v2') format(%12.2f)
	insert_into_file using table2.csv, key(dtaxrate_onsite0_sd) value(`v3') format(%12.2f)
	insert_into_file using table2.csv, key(dtaxrate_onsite1_sd) value(`v4') format(%12.2f)
	
	* tax_savings
	local v1 = stats1[1,6]
	local v2 = stats2[1,6]
	local v3 = stats1[2,6]
	local v4 = stats2[2,6]
	insert_into_file using table2.csv, key(tax_savings0_mean) value(`v1') format(%12.0fc)
	insert_into_file using table2.csv, key(tax_savings1_mean) value(`v2') format(%12.0fc)
	insert_into_file using table2.csv, key(tax_savings0_sd) value(`v3') format(%12.0fc)
	insert_into_file using table2.csv, key(tax_savings1_sd) value(`v4') format(%12.0fc)
	
	* tax_savings_per_unit
	local v1 = stats1[1,7]
	local v2 = stats2[1,7]
	local v3 = stats1[2,7]
	local v4 = stats2[2,7]
	insert_into_file using table2.csv, key(tax_savings_per_unit0_mean) value(`v1') format(%12.0fc)
	insert_into_file using table2.csv, key(tax_savings_per_unit1_mean) value(`v2') format(%12.0fc)
	insert_into_file using table2.csv, key(tax_savings_per_unit0_sd) value(`v3') format(%12.0fc)
	insert_into_file using table2.csv, key(tax_savings_per_unit1_sd) value(`v4') format(%12.0fc)
	
	* unitstotal
	local v1 = stats1[1,8]
	local v2 = stats2[1,8]
	local v3 = stats1[2,8]
	local v4 = stats2[2,8]
	insert_into_file using table2.csv, key(unitstot0_mean) value(`v1') format(%12.2f)
	insert_into_file using table2.csv, key(unitstot1_mean) value(`v2') format(%12.2f)
	insert_into_file using table2.csv, key(unitstot0_sd) value(`v3') format(%12.2f)
	insert_into_file using table2.csv, key(unitstot1_sd) value(`v4') format(%12.2f)
	
	* unitsres
	local v1 = stats1[1,9]
	local v2 = stats2[1,9]
	local v3 = stats1[2,9]
	local v4 = stats2[2,9]
	insert_into_file using table2.csv, key(unitsres0_mean) value(`v1') format(%12.2f)
	insert_into_file using table2.csv, key(unitsres1_mean) value(`v2') format(%12.2f)
	insert_into_file using table2.csv, key(unitsres0_sd) value(`v3') format(%12.2f)
	insert_into_file using table2.csv, key(unitsres1_sd) value(`v4') format(%12.2f)
	
	* numfloors
	local v1 = stats1[1,10]
	local v2 = stats2[1,10]
	local v3 = stats1[2,10]
	local v4 = stats2[2,10]
	insert_into_file using table2.csv, key(numfloors0_mean) value(`v1') format(%12.2f)
	insert_into_file using table2.csv, key(numfloors1_mean) value(`v2') format(%12.2f)
	insert_into_file using table2.csv, key(numfloors0_sd) value(`v3') format(%12.2f)
	insert_into_file using table2.csv, key(numfloors1_sd) value(`v4') format(%12.2f)
	
	* rental
	local v1 = stats1[1,11]
	local v2 = stats2[1,11]
	local v3 = stats1[2,11]
	local v4 = stats2[2,11]
	insert_into_file using table2.csv, key(rental0_mean) value(`v1') format(%12.3f)
	insert_into_file using table2.csv, key(rental1_mean) value(`v2') format(%12.3f)
	insert_into_file using table2.csv, key(rental0_sd) value(`v3') format(%12.3f)
	insert_into_file using table2.csv, key(rental1_sd) value(`v4') format(%12.3f)
	
	* taxrate
	local v1 = stats1[1,12]
	local v2 = stats2[1,12]
	local v3 = stats1[2,12]
	local v4 = stats2[2,12]
	insert_into_file using table2.csv, key(taxrate0_mean) value(`v1') format(%12.2f)
	insert_into_file using table2.csv, key(taxrate1_mean) value(`v2') format(%12.2f)
	insert_into_file using table2.csv, key(taxrate0_sd) value(`v3') format(%12.2f)
	insert_into_file using table2.csv, key(taxrate1_sd) value(`v4') format(%12.2f)
	
	* Number of observations
	tabstat inclusionary_onsite, by(inclusionary_onsite) stats(n) save
	mat n1 = r(Stat1)
	mat n2 = r(Stat2)
	local v1 = n1[1,1]
	local v2 = n2[1,1]
	insert_into_file using table2.csv, key(n0) value(`v1') format(%12.0fc)
	insert_into_file using table2.csv, key(n1) value(`v2') format(%12.0fc)
	
	* Number of observations
	expand unitsres
	tabstat inclusionary_onsite, by(inclusionary_onsite) stats(n) save
	mat n1 = r(Stat1)
	mat n2 = r(Stat2)
	local v1 = n1[1,1]
	local v2 = n2[1,1]
	insert_into_file using table2.csv, key(n0_units) value(`v1') format(%12.0fc)
	insert_into_file using table2.csv, key(n1_units) value(`v2') format(%12.0fc)
	
********************************************************************************
* Create table
********************************************************************************
	
	cat table2.csv

	cap erase "$tables/output/table2.tex"

	cd "$code/modules"
	capture table_from_tpl, t("$tables/templates/table2.tex") ///
							r("$tables/estimates/table2.csv") ///
							o("$tables/output/table2.tex") 
			
	capture table_from_tpl, t("$tables/templates/table2.tex") ///
							r("$tables/estimates/table2.csv") ///
							o("$tables_overleaf/table2.tex") 
							
	exit
